package com.baoming.web.action;

import java.util.List;

import org.apache.log4j.Logger; 

import com.baoming.container.ServiceContainer;
import com.baoming.domain.Properties;
import com.baoming.domain.Singup;
import com.baoming.domain.SingupEnter;
import com.baoming.service.IPropertiesService;
import com.baoming.service.ISingupEnterService;
import com.baoming.service.ISingupService; 
import com.baoming.tool.ExcellentTool;
import com.baoming.tool.LogerTool;
import com.baoming.tool.MemberTool;
import com.baoming.tool.XmlTool;
import com.baoming.util.PageBeanUtil;
import com.baoming.util.PageInfo;
import com.baoming.util.ServiceTool;
import com.baoming.util.Struts2UploadUtil;
import com.baoming.util.UploadUtil;
import com.opensymphony.xwork2.ModelDriven;

@SuppressWarnings("serial")
public class SingupAction extends BaseAction implements ModelDriven<Singup>{
	
	private ISingupService singupService = (ISingupService) ServiceContainer.getService(ISingupService.SERVICE) ; 
	private IPropertiesService propService = (IPropertiesService) ServiceContainer.getService(IPropertiesService.SERVICE) ; 
	private ISingupEnterService enterService = (ISingupEnterService) ServiceContainer.getService(ISingupEnterService.SERVICE) ; 
	private Singup singup = new Singup() ; 
	
	private Logger logger = Logger.getLogger(SingupAction.class.getName());
	
	/**
	 * 立即结束比赛
	 * @return
	 */
	public String singupEnd(){
		
		LogerTool.info(logger, request, "立即结束比赛") ; 
		
		//从页面得到比赛的id 
		String id = request.getParameter("id") ; 
		 
		if(id  != null){
			//得到当前时间
			String nowTime = ServiceTool.getNowDate() ; 
			//查询要结束的报名 
			Singup singup = singupService.selectSingUpById(id) ; 
			//设置结束时间为当前时间
			singup.setEndTime(nowTime) ; 
			//更新时间到数据库
			singupService.modifySingup(singup) ; 
		}
		
		return "singupEnd" ; 
	}
	
	/**
	 * 标记用户
	 * @return
	 */
	public String tagEnter(){
		LogerTool.info(logger, request, "标记用户") ; 
		String id = request.getParameter("id") ; 
		SingupEnter enter = enterService.selectEnterById(id) ; 
		if(enter != null){
			System.out.println(enter.getTag());
			int tag = 0 ; 
			if(enter.getTag() == null || enter.getTag() == 0){
				tag = 1 ; 
			}
			enterService.tagEnter(id, tag) ; 
		}
		return "tagEnter" ; 
	}
	
	/**
	 * 导出全部
	 * @return
	 */
	public String exportAll(){
		
		LogerTool.info(logger, request, "导出全部比赛") ; 
		
		String memberId = MemberTool.getGlobalMemberId(request) ; 
		//得出所有报名比赛
		List<Singup> singups = singupService.selectSingupWithPage(memberId) ; 
		
		for(Singup sing : singups){
			String id = sing.getId() ; 
			//查询出所有报名人数
			List<SingupEnter> enters = enterService.selectSingupEnterBySingupId(id) ; 
			//得到所有报名属性
			List<Properties> props = propService.getPropertiesBySingupId(id); 
			  
			//导出成为Excellent,保存于本地
			String excelPath = ExcellentTool.createExcel(enters , props , id) ; 
			
			//更新保存路径
			sing.setCodePath(excelPath) ; 
			singupService.modifySingup(sing) ; 
		}
		
		return "exportAll"; 
	}
	
	/**
	 * 报名导出
	 */
	public String exportEnter(){
		
		//得到比赛的id
		String id = request.getParameter("id") ; 
		if(id == null|| "".equals(id)){
			return "exportEnter" ; 
		}
		
		Singup sing = singupService.selectSingUpById(id) ; 
		
		//查询出所有报名人数
		List<SingupEnter> enters = enterService.selectSingupEnterBySingupId(id) ; 
		//得到所有报名属性
		List<Properties> props = propService.getPropertiesBySingupId(id); 
		  
		//导出成为Excellent,保存于本地
		String excelPath = ExcellentTool.createExcel(enters , props , id) ; 
		
		//更新保存路径
		sing.setCodePath(excelPath) ; 
		singupService.modifySingup(sing) ; 
		
		LogerTool.info(logger, request, "导出" + sing.getTitle() + " 信息 ") ; 
		
		return "exportEnter" ; 
	}
	
	/**
	 * 删除报名信息
	 * @return
	 */
	public String removeEnter(){
		
		String id = request.getParameter("id");   
		LogerTool.info(logger, request, "删除报名信息 ") ; 
		if(id != null){
			enterService.deleteSingupEnter(id); 
		}
		return "removeEnter" ; 
	}
	
	/**
	 * 报名修改保存
	 * @return
	 */
	public String update(){
		
		//得到修改报名的id 
		String id = singup.getId() ; 
		if(id != null){
			
			Singup sing = singupService.selectSingUpById(id) ; 
			LogerTool.info(logger, request, "报名"+sing.getTitle()+"修改为"+ singup.getTitle()) ; 
			sing.setEndTime(singup.getEndTime().trim()+" 23:55:50") ; 
			sing.setContent(singup.getContent()) ; 
			sing.setTitle(singup.getTitle()) ; 
			 
			//报名海报的修改
			if(Struts2UploadUtil.fileUtil(singup.getPic())){
				String picPath = UploadUtil.uploadSimpleFile(singup.getPic(), singup.getPicFileName(), "upload") ; 
				sing.setPicPath(picPath) ;   
			} 
			 
			sing.setCode(singup.getCode()) ; 
			singupService.modifySingup(sing) ; 
			request.getSession().setAttribute("singup_tip", 2) ; 
		}
		
		return "update" ; 
	}
	
	/**
	 * 报名修改
	 * @return
	 */
	public String modify(){
		//得到报名信息
		String id = request.getParameter("id") ; 
		if(id != null){
			Singup singup = singupService.selectSingUpById(id) ; 
			request.setAttribute("singup", singup) ; 
		}
		return SUCCESS ; 
	}
  
	//查询报名信息
	public String search(){
		
		//得到比赛的id 
		String singupId = request.getParameter("singupId") ; 
		 
		//查询出比赛报名的信息
		PageInfo pageInfo = new PageInfo(request) ; 
		List<SingupEnter> enters = null ; 
		
		String keyword = request.getParameter("keyword") ;  
		if(keyword != null && keyword.length() > 0 && filterXml(keyword)){
			/*
			 * 分页搜索 
			 */
			keyword = ServiceTool.iosToUtf8(keyword) ; 
			enters = enterService.selectSingupEnterBySingupId(keyword.trim() , singupId , pageInfo) ; 
			request.setAttribute("keyword", keyword) ; 
		}else{
			/*
			 * 分页
			 */
			enters = enterService.selectSingupEnterBySingupId(singupId , pageInfo) ; 
		}
		
		//设置分页
		PageBeanUtil.setPage(pageInfo, request) ;  
		
		//解析XML值  
		XmlTool.createList(enters) ; 
		request.setAttribute("enters", enters) ;
		 
		//得到表单头，以最终的信息为准
		List<Properties> headerProp = propService.getPropertiesBySingupId(singupId) ; 
		request.setAttribute("headers", headerProp) ; 
		 
		//得出所有报名比赛
		String memberId = MemberTool.getGlobalMemberId(request) ; 
		List<Singup> singups = singupService.selectSingupWithPage(memberId) ; 
		request.setAttribute("singups", singups) ; 
		
		//设置当前比赛的ID
		request.setAttribute("currentSingupId", singupId) ; 
		 
		return "search" ; 
	}
	
	/**
	 * 过渡Xml的字符 
	 * @param keyword
	 * @return
	 */
	private boolean filterXml(String keyword) {
		// TODO Auto-generated method stub
		String[] xmlProp = {"<?xml version encoding=utf-8?>" , "xml_body" , "name" , "value" , "time" , "property"} ; 
		for(String str : xmlProp){
			if(str.contains(keyword)){
				return false ; 
			}
		}
		return true;
	}

	/**
	 * 报名导出
	 * @return
	 */
	public String export(){
		
		//得到会员的Id
		String memberId = MemberTool.getGlobalMemberId(request) ; 
		//得出所有报名比赛
		List<Singup> singups = singupService.selectSingupWithPage(memberId) ; 
		request.setAttribute("singups", singups) ; 
		
		//处理所有报名的ID
		String ids = "'_'" ; 
		for(Singup singup : singups){
			ids += (","+ "'"+singup.getId()+"'")  ; 
		}
		//得到所有已报名信息
		if(ids.length()>10){
			  
			//查询最近报名
			List<SingupEnter> enters = enterService.selectReSingupEnterBySingupIds(ids); 
			  
			for(SingupEnter enter : enters){
				Singup singup = singupService.selectSingUpById(enter.getSingupId()) ; 
				
				//得到报名的用户名 (统一属性name)
				String enterName = propService.findSingProp("name" , enter.getXmlValue()) ; 
				enter.setEnterName(enterName) ; 
				
				//设置比赛的名字
				enter.setSingupName(singup.getTitle()); 
			}
			//查询到所有已报名
			request.setAttribute("Allenters", enters) ; 
//			PageBeanUtil.setPage(pageInfo, request) ;  
		}
		return SUCCESS ; 
	}
	
	/**
	 * 报名属性管理
	 * @return
	 */
	public String properties(){
		/*
		 * 得出所有报名
		 */
		String memberId = MemberTool.getGlobalMemberId(request) ; 
		List<Singup> singups = singupService.selectSingupWithPage(memberId) ; 
		 
		request.setAttribute("singups", singups) ; 
		
		//得到所有属性值 
		List<Properties> props = propService.listProperties() ; 
		request.setAttribute("props", props) ; 
		
		return SUCCESS ; 
	}
	
	@Override
	public String edit() {
		String memberId = MemberTool.getGlobalMemberId(request) ; 
		// 查询出用户所有报名 
		List<Singup> singups = singupService.selectSingupWithPage(memberId) ; 
		//如果用户的现有报名达到50 则不上限
		if(singups != null && singups.size() >= 50){
			return "editError" ; 
		}
		return SUCCESS ;
	}

	/**
	 * 信息展示页面
	 * @return
	 */
	public String home(){
		
		return SUCCESS ; 
	}
	
	/**
	 * 报名管理
	 */
	@Override
	public String manager() {
		// TODO Auto-generated method stub
		String memberId = MemberTool.getGlobalMemberId(request) ; 
		PageInfo pageInfo = new PageInfo(request) ; 
		List<Singup> singups = null ; 
		
		String keyword = request.getParameter("keyword") ;
		if( keyword != null && keyword.trim().length()>0){
			keyword = ServiceTool.iosToUtf8(keyword) ; 
			/**
			 * 分页搜索 
			 */
			singups = singupService.selectSingupWithPage(keyword.trim() , pageInfo , memberId) ; 
			request.setAttribute("keyword", keyword) ; 
			
		}else{
			/**
			 * 默认搜索 
			 */
			singups = singupService.selectSingupWithPage(pageInfo , memberId) ; 
			
		}
		
		//处理时间跟报名人数
		for(Singup singup : singups){
			//时间对比
			int state = ServiceTool.timeCompain(singup.getEndTime(), ServiceTool.getNowDate()) ; 
			//设置时间
			singup.setState(state) ; 
			//人数处理
			int num = enterService.selectCountEnter(singup.getId()) ; 
			singup.setNum(num) ; 
		}
		
		request.setAttribute("singups", singups) ; 
		PageBeanUtil.setPage(pageInfo, request); 
		return SUCCESS ;
	}
	
	/**
	 * 删除比赛 
	 */
	@Override
	public String remove() {
		// TODO Auto-generated method stub
		String ids[] = singup.getIds() ; 
		for(String id : ids){
			Singup singup = singupService.selectSingUpById(id) ; 
			singup.setIsDelete(0) ; 
			singupService.modifySingup(singup);
			
			//设置报名的提示信息
			request.getSession().setAttribute("singup_tip", 1) ; 
		}
		return "remove";
	}

	@Override
	public String add() {
		// TODO Auto-generated method stub
		
		String memberId = MemberTool.getGlobalMemberId(request) ; 

		if(Struts2UploadUtil.fileUtil(singup.getPic())){
			String picPath = UploadUtil.uploadSimpleFile(singup.getPic(), singup.getPicFileName(), "upload") ; 
			singup.setPicPath(picPath) ; 
		}
		 
		singup.setEndTime(singup.getEndTime().trim()+" 23:55:50") ; 
		singup.setAddTime(ServiceTool.getNowDate()) ; 
		singup.setIsAllow(1) ; 
		singup.setIsDelete(1) ; 
		singup.setMemberId(memberId) ; 
		
		singupService.saveSingup(singup) ; 
		
		return "add" ;
	}

	@Override
	public String removeGroup() {
		// TODO Auto-generated method stub
		return null;
	}

	public Singup getModel() {
		// TODO Auto-generated method stub
		return singup ;
	}

	 
	
}
